<?php
require_once('wpfunctions.php');
header("Content-type: application/json");
$type = $_REQUEST['type'];
if(!$type) {
  $type = 'square';
}
if($type=='modern') {
  $regex = '/.json$/';
} else {
  $regex = '/.gabc$/';
}
$rootPaths = ip_allFiles("engravings/$type/");
$result = array();

foreach ($rootPaths as $i => $root) {
  if(is_dir($root)) {
    $roots = ip_scandir($root);
    foreach($roots as $r) {
      if(is_dir($root.$r) && strpos($r,'tones')!==FALSE) {
        $r .= '/';
        $entries = ip_scandir($root.$r);
        foreach($entries as $v) {
          if(!is_dir($root.$r.$v) && preg_match($regex,$v)) {
            $result[] = $r.$v;
          }
        }
      }
    }
  }
}

echo json_encode(array_unique($result));
?>